﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Project;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Reports;

namespace ManagementApi.Controllers.ManagerPannel
{
    /// <summary>
    /// 项目月统计报表
    /// </summary>
    [Route("/api/v1/manager/reportProjectMonth")]
    [ApiController]
    [Authorize(Roles = "Manager")]
    public class ReportProjectMonthController : ControllerBase
    {
        private readonly ProjectManagerApplicationContext applicationContext;
        private readonly IMapper mapper;
        private readonly ReportProjectMonthService reportProjectMonthService;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="applicationContext">上下文</param>
        /// <param name="mapper">mapper映射</param>
        /// <param name="ReportProjectMonthService">主业务service</param>
        public ReportProjectMonthController(ProjectManagerApplicationContext applicationContext, IMapper mapper, ReportProjectMonthService reportProjectMonthService)
        {
            this.applicationContext = applicationContext;
            this.mapper = mapper;
            this.reportProjectMonthService = reportProjectMonthService;
        }

        /***进入模版管理可以修改模版***/

        /// <summary>
        /// 获取项目月统计报表列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<ReportProjectMonthDto>> GetReportProjectMonths([FromQuery] ReportProjectMonthQuery query, int pageNum = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            var data = reportProjectMonthService.GetReportProjectMonths(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<ReportProjectMonthDto>>.Success(dtos);
        }

        /// <summary>
        /// 项目月统计报表信息表单数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        [PermissionCheck("reportProjectMonth:details")]
        public ApiResult<ReportProjectMonthEditForm> GetRole(int id)
        {
            var data = reportProjectMonthService.Get(id);
            if (data == null)
                return ApiResult<ReportProjectMonthEditForm>.Failure("项目月统计报表信息数据不存在");
            return ApiResult<ReportProjectMonthEditForm>.Success(data.AsForm());
        }
    }
}